import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import "qrc:/qml/styles" as Styles

Rectangle {
    id: root
    property int agvNumber: 0
    property string name: ""
    property string ipAddress: ""
    property int type: 0
    property string currentStation: ""
    property string lastStation: ""
    property string targetStation: ""
    property string currentMap: ""
    property string currentJob: ""
    property real waypointX: 0
    property real waypointY: 0
    property real heading: 0
    property real confidence: 0
    property int batteryLevel: 0
    property bool isOnline: false
    property bool isCharging: false
    property bool isLocked: false
    property bool isBlocked: false
    property int mode: 0
    property int taskStatus: 0
    property int taskType: 0
    property bool isControlled: false
    property string errorMessage: ""
    property var unfinishedPath: []
    property var finishedPath: []
    property int pathLength: 0
    property bool isBusy: false

    signal clicked()

    width: 320
    implicitHeight: content.implicitHeight + Styles.Theme.spacingNormal * 2
    color: mouseArea.pressed ? Qt.darker(Styles.Theme.surface, 1.08) :
           mouseArea.containsMouse ? Qt.lighter(Styles.Theme.surface, 1.03) : Styles.Theme.surface
    radius: Styles.Theme.radiusNormal
    border.color: mouseArea.containsMouse ? Styles.Theme.primary : Styles.Theme.border
    border.width: 1

    // 左侧状态指示条
    Rectangle {
        id: statusIndicator
        width: 4
        height: parent.height - 2
        anchors.left: parent.left
        anchors.leftMargin: 1
        anchors.verticalCenter: parent.verticalCenter
        radius: 2
        color: getStatusColor()
    }

    // 平滑过渡动画
    Behavior on color {
        ColorAnimation { duration: 150 }
    }
    Behavior on border.color {
        ColorAnimation { duration: 150 }
    }
    Behavior on scale {
        NumberAnimation { duration: 100; easing.type: Easing.OutQuad }
    }

    scale: mouseArea.pressed ? 0.98 : 1.0
    transformOrigin: Item.Center

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: true
        cursorShape: Qt.PointingHandCursor
        onClicked: root.clicked()
    }

    ColumnLayout {
        id: content
        anchors.fill: parent
        anchors.leftMargin: Styles.Theme.spacingNormal + 8
        anchors.rightMargin: Styles.Theme.spacingNormal
        anchors.topMargin: Styles.Theme.spacingNormal
        anchors.bottomMargin: Styles.Theme.spacingNormal
        spacing: Styles.Theme.spacingSmall

        // 第一行：名称和在线状态
        RowLayout {
            Layout.fillWidth: true
            spacing: Styles.Theme.spacingSmall

            Text {
                text: name.length > 0 ? name : qsTr("AGV #") + agvNumber
                color: Styles.Theme.textPrimary
                font.pixelSize: Styles.Theme.fontSizeNormal
                font.weight: Font.DemiBold
                Layout.fillWidth: true
                elide: Text.ElideRight
            }

            // 在线状态标签
            Rectangle {
                width: statusLabel.width + 16
                height: 22
                radius: 11
                color: isOnline ? Qt.alpha(Styles.Theme.success, 0.15) : Qt.alpha(Styles.Theme.danger, 0.15)

                Text {
                    id: statusLabel
                    anchors.centerIn: parent
                    text: isOnline ? qsTr("在线") : qsTr("离线")
                    color: isOnline ? Styles.Theme.success : Styles.Theme.danger
                    font.pixelSize: 10
                    font.weight: Font.Medium
                }
            }
        }

        // 第二行：IP地址 + 状态标签
        RowLayout {
            Layout.fillWidth: true
            spacing: Styles.Theme.spacingSmall

            Text {
                text: ipAddress
                color: Styles.Theme.textSecondary
                font.pixelSize: Styles.Theme.fontSizeSmall
                elide: Text.ElideRight
            }

            Item { Layout.fillWidth: true }

            // 状态标签组
            RowLayout {
                spacing: 6

                // 忙碌状态
                Rectangle {
                    visible: isBusy
                    width: busyText.width + 10
                    height: 18
                    radius: 9
                    color: Qt.alpha(Styles.Theme.info, 0.15)

                    Text {
                        id: busyText
                        anchors.centerIn: parent
                        text: qsTr("忙碌")
                        color: Styles.Theme.info
                        font.pixelSize: 10
                        font.weight: Font.Medium
                    }
                }

                // 调度状态
                Rectangle {
                    visible: isControlled
                    width: controlText.width + 10
                    height: 18
                    radius: 9
                    color: Qt.alpha(Styles.Theme.primary, 0.15)

                    Text {
                        id: controlText
                        anchors.centerIn: parent
                        text: qsTr("调度")
                        color: Styles.Theme.primary
                        font.pixelSize: 10
                        font.weight: Font.Medium
                    }
                }

                // 锁定状态
                Rectangle {
                    visible: isLocked
                    width: lockText.width + 10
                    height: 18
                    radius: 9
                    color: Qt.alpha(Styles.Theme.warning, 0.15)

                    Text {
                        id: lockText
                        anchors.centerIn: parent
                        text: qsTr("锁定")
                        color: Styles.Theme.warning
                        font.pixelSize: 10
                        font.weight: Font.Medium
                    }
                }
            }
        }

        // 第三行：电池信息 + 位置信息
        RowLayout {
            Layout.fillWidth: true
            spacing: Styles.Theme.spacingSmall

            // 电池显示
            RowLayout {
                spacing: 6

                // 电池图标
                Rectangle {
                    width: 24
                    height: 11
                    radius: 2
                    color: "transparent"
                    border.color: getBatteryColor()
                    border.width: 1.5

                    Rectangle {
                        anchors.left: parent.left
                        anchors.leftMargin: 2
                        anchors.verticalCenter: parent.verticalCenter
                        width: Math.max(0, (parent.width - 5) * batteryLevel / 100)
                        height: parent.height - 4
                        radius: 1
                        color: getBatteryColor()
                    }

                    // 电池头
                    Rectangle {
                        anchors.left: parent.right
                        anchors.verticalCenter: parent.verticalCenter
                        width: 2
                        height: 4
                        radius: 1
                        color: getBatteryColor()
                    }
                }

                Text {
                    text: batteryLevel + "%"
                    color: getBatteryColor()
                    font.pixelSize: Styles.Theme.fontSizeSmall
                    font.weight: Font.Medium
                }

                // 充电指示
                Text {
                    visible: isCharging
                    text: "\u26A1"
                    font.pixelSize: 10
                    color: Styles.Theme.warning
                }
            }

            Item { Layout.fillWidth: true }

            // 位置信息
            RowLayout {
                spacing: 4

                Text {
                    text: currentStation || "-"
                    color: Styles.Theme.textSecondary
                    font.pixelSize: Styles.Theme.fontSizeSmall
                    elide: Text.ElideRight
                }

                Text {
                    text: "\u2192"
                    color: Styles.Theme.textSecondary
                    font.pixelSize: 10
                    opacity: targetStation ? 1.0 : 0.3
                }

                Text {
                    text: targetStation || "-"
                    color: targetStation ? Styles.Theme.primary : Styles.Theme.textSecondary
                    font.pixelSize: Styles.Theme.fontSizeSmall
                    font.weight: targetStation ? Font.Medium : Font.Normal
                    elide: Text.ElideRight
                }
            }
        }

        // 错误信息
        Rectangle {
            visible: errorMessage.length > 0
            Layout.fillWidth: true
            implicitHeight: errorRow.implicitHeight + Styles.Theme.spacingSmall * 2
            color: Qt.alpha(Styles.Theme.danger, 0.1)
            radius: Styles.Theme.radiusSmall
            border.color: Qt.alpha(Styles.Theme.danger, 0.3)
            border.width: 1

            RowLayout {
                id: errorRow
                anchors.fill: parent
                anchors.margins: Styles.Theme.spacingSmall
                spacing: Styles.Theme.spacingSmall

                Text {
                    text: "\u26A0"
                    font.pixelSize: 12
                    color: Styles.Theme.danger
                }

                Text {
                    text: errorMessage
                    color: Styles.Theme.danger
                    font.pixelSize: Styles.Theme.fontSizeSmall
                    wrapMode: Text.WordWrap
                    Layout.fillWidth: true
                }
            }
        }
    }

    // 辅助函数
    function getStatusColor() {
        if (!isOnline) return Styles.Theme.textSecondary
        if (isCharging) return Styles.Theme.warning
        if (isBusy || isControlled) return Styles.Theme.info
        if (errorMessage.length > 0) return Styles.Theme.danger
        return Styles.Theme.success
    }

    function getBatteryColor() {
        if (batteryLevel <= 20) return Styles.Theme.danger
        if (batteryLevel <= 50) return Styles.Theme.warning
        return Styles.Theme.success
    }
}
